package com.budwk.app.enterprise.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.enterprise.models.WxMainPersonnel;
import com.budwk.app.enterprise.services.WxmainpersonnelService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;
import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/enterprise/enterprise/main/personnel")
@ApiDefinition(tag = "主要人员实体")
@Slf4j
public class WxmainpersonnelController{
    @Inject
    private WxmainpersonnelService wxMainPersonnelService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询主要人员实体")
    @ApiFormParams(
        value = {
            @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
            @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
            @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
            @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
        },
        implementation = WxMainPersonnel.class
    )
    @ApiResponses(
    implementation = WxMainPersonnel.class
    )
    @SaCheckPermission("enterprise.enterprise.wx.main.personnel")
    public Result<?> list(@Param("..")WxMainPersonnel bean,@Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getFullName() !=null && Strings.isNotBlank(bean.getFullName().toString())) {
            cnd.and("fullName","=",bean.getFullName());
        }

        if (bean.getSex() !=null && Strings.isNotBlank(bean.getSex().toString())) {
            cnd.and("sex","=",bean.getSex());
        }
//
//        if (bean.getAge()!=null && Strings.isNotBlank(bean.getAge())) {
//            cnd.and("age","=",bean.getAge());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("academicBackground","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("post","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("emolument","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("numberSharesHeld","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("thisTermOffice","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("dateAnnouncement","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("introduceBriefly","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("shareholdingRatio","=",bean.getnull());
//        }
//
//        if (bean.getnull() !=null && Strings.isNotBlank(bean.getnull().toString())) {
//            cnd.and("finalBeneficiaryShares","=",bean.getnull());
//        }
//
        return Result.data(wxMainPersonnelService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增主要人员实体")
    @ApiFormParams(
        implementation = WxMainPersonnel.class
    )
    @ApiResponses
    @SLog(tag = "新增主要人员实体", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.main.personnel.create")
    public Result<?> create(@Param("..")WxMainPersonnel  wxMainPersonnel, HttpServletRequest req) {
         wxMainPersonnel.setCreatedBy(SecurityUtil.getUserId());
        wxMainPersonnelService.insert( wxMainPersonnel);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改主要人员实体")
    @ApiFormParams(
        implementation = WxMainPersonnel.class
    )
    @ApiResponses
    @SLog(tag = "修改主要人员实体", value = "${args[0].id}")
    @SaCheckPermission("enterprise.enterprise.wx.main.personnel.update")
    public Result<?> update(@Param("..")WxMainPersonnel  wxMainPersonnel, HttpServletRequest req) {
         wxMainPersonnel.setUpdatedBy(SecurityUtil.getUserId());
        wxMainPersonnelService.updateIgnoreNull( wxMainPersonnel);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取主要人员实体")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SaCheckPermission("enterprise.enterprise.wx.main.personnel")
    public Result<?> getData(@Param("id")String id, HttpServletRequest req) {
        WxMainPersonnel  wxMainPersonnel = wxMainPersonnelService.fetch(id);
        if ( wxMainPersonnel == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data( wxMainPersonnel);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除主要人员实体")
    @ApiFormParams(
        {
            @ApiFormParam(name = "id", description = "唯一标识",required = true, check = true)
        }
    )
    @SLog(tag = "删除主要人员实体", value = "${args[0]}")
    @SaCheckPermission("enterprise.enterprise.wx.main.personnel.delete")
    public Result<?> delete(@Param("id")String id, HttpServletRequest req) {
        WxMainPersonnel  wxMainPersonnel = wxMainPersonnelService.fetch(id);
        if ( wxMainPersonnel == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxMainPersonnelService.delete(id);
        req.setAttribute("_slog_msg",  wxMainPersonnel.getId());
        return Result.success();
    }
}
